package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.Person;
import service.IPersonService;
import service.impl.PersonServiceImpl;
import util.LayData;

@Controller
@RequestMapping("/Person")
public class PersonController {

    @Autowired
    private IPersonService personService = new PersonServiceImpl();

    @GetMapping("/Persons")
    @ResponseBody
    public LayData getAllPersons() {
        LayData ld = new LayData();

        ld.setCode(0);
        ld.setMsg("");
        ld.setData(personService.getAllPerson());

        return ld;
    }

    @PostMapping("/PersonByPid/{pid}")
    @ResponseBody
    public Person getPersonByPid(@PathVariable(value = "pid") int pid) {
        return personService.getPersonByPid(pid);
    }

    @PostMapping("/PersonUpdate")
    @ResponseBody
    public LayData updatePerson(@RequestBody Person p) {
        LayData ld = new LayData();

        boolean b = personService.updatePerson(p);

        if (b) {
            ld.setCode(0);
        }
        return ld;
    }

    @PostMapping("/PersonSave")
    @ResponseBody
    public LayData savePerson(@RequestBody Person p) {
        LayData ld = new LayData();

        boolean b = personService.savePerson(p);
        if (b){
            ld.setCode(0);
        }
        return ld;
    }

    @PostMapping("/PersonLogin")
    @ResponseBody
        public Person login(@RequestBody Person p){

            return personService.login(p.getTel(), p.getPass());
    }
}
